package cn.demomaster.quickui.view.floatview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.demomaster.quickcompat.util.DisplayUtil;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import cn.demomaster.quicklogger.QdLog;

public class QuickRulerView extends View {
    public QuickRulerView(Context context) {
        super(context);
    }

    public QuickRulerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public QuickRulerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public QuickRulerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getMeasuredHeight();
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        for (int i=0;i<height/2;i++){
            int y = i*2;
            int w = DisplayUtil.dip2px(getContext(),5);
            if(i%5==0){
                w = DisplayUtil.dip2px(getContext(),10);
            }
            if(i%10==0){
                w = DisplayUtil.dip2px(getContext(),30);
            }
            canvas.drawLine(0,y,w,y,paint);
        }
        paint.setColor(Color.RED);
        canvas.drawLine(0,dy,getMeasuredWidth(),dy,paint);
        canvas.drawText("Y="+dy,getMeasuredWidth()/2,dy,paint);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        dy = (int) event.getY();
        QdLog.println("dispatchTouchEvent eY="+event.getY());
        postInvalidate();
        return super.dispatchTouchEvent(event);
    }

    int dy = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        View mDecorView = ((Activity)getContext()).getWindow().getDecorView();
         View contentView = mDecorView.findViewById(android.R.id.content);
         if(contentView!=null) {
             QdLog.println("onTouchEvent2 eY="+event.getY());
             return contentView.dispatchTouchEvent(event);
            //return contentView.onTouchEvent(event);
         }
        /*if(mDecorView instanceof ViewGroup){
            ViewGroup mDecorViewGroup = (ViewGroup) mDecorView;
            for(int i=0;i<mDecorViewGroup.getChildCount();i++){
                View view = mDecorViewGroup.getChildAt(i);
                if(view.getId() != cn.demomaster.quickrouter.R.id.quick_float_contentview_id){
                    view.setBackgroundColor(Color.RED);
                    QdLog.println(view);
                    ((Activity)getContext()).getWindow().getDecorView().onTouchEvent(event);
                }
            }
        }*/
        return true;
        //return super.onTouchEvent(event);
    }
}
